import Router from 'koa-router'
import inspectionModel from '../dbs/models/inspection'

let router = new Router({ prefix: '/inspect' })

// 纪检员上传表
router.get('/inspectionUp', async ctx => {
  // 用于查看有没有当天已经上传过的
  let where = {
    tower: ctx.query.tower,
    dorm: ctx.query.dorm
  }
  let isUp = await inspectionModel.find(where, null)
  // console.log(isUp)
  if (isUp.length !== 0) {
    let oldTime = ''
    isUp.forEach(valid => {
      if (valid.time === ctx.query.time) {
        oldTime = true
      } else {
        oldTime = false
      }
    })
    // console.log(oldTime)
    if (oldTime) {
      await inspectionModel.updateOne({
        tower: ctx.query.tower,
        dorm: ctx.query.dorm,
        time: ctx.query.time
      }, {
        hygiene: ctx.query.hygiene,
        culture: ctx.query.culture,
        desc: ctx.query.desc
      }, (err, res) => {
        if (err) {
          ctx.body = {
            meta: {
              msg: '上传失败',
              code: -1
            }
          }
        } else if (res.n === 0) {
          ctx.body = {
            meta: {
              msg: '上传失败',
              code: -1
            }
          }
        } else {
          ctx.body = {
            meta: {
              msg: '上传成功',
              code: 0
            }
          }
        }
      })
    } else {
      const inspection = new inspectionModel({
        inspector: ctx.query.inspector,
        tower: ctx.query.tower,
        dorm: ctx.query.dorm,
        class: ctx.query.class,
        hygiene: ctx.query.hygiene,
        culture: ctx.query.culture,
        score: ctx.query.score,
        desc: ctx.query.desc,
        time: ctx.query.time
      })
      let code
      try {
        await inspection.save()
        code = 0
      } catch (e) {
        code = -1
      }
      if (code !== -1) {
        ctx.body = {
          meta: {
            msg: '上传成功',
            code: code
          }
        }
      } else {
        ctx.body = {
          meta: {
            msg: '上传失败',
            code: code
          }
        }
      }
    }
  } else {
    // console.log('添加2')
    const inspection = new inspectionModel({
      inspector: ctx.query.inspector,
      tower: ctx.query.tower,
      dorm: ctx.query.dorm,
      class: ctx.query.class,
      hygiene: ctx.query.hygiene,
      culture: ctx.query.culture,
      score: ctx.query.score,
      desc: ctx.query.desc,
      time: ctx.query.time
    })
    // console.log(inspection)
    let code
    try {
      await inspection.save()
      code = 0
    } catch (e) {
      code = -1
    }
    if (code !== -1) {
      ctx.body = {
        meta: {
          msg: '上传成功',
          code: code
        }
      }
    } else {
      ctx.body = {
        meta: {
          msg: '上传失败',
          code: code
        }
      }
    }
  }
})
// 查询表信息
router.get('/IPTNfind', async ctx => {
  let where = {
    tower: ctx.query.tower,
    dorm: ctx.query.dorm
  }
  let result = await inspectionModel.find(where).sort({ "time": -1 })
  if (result.length !== 0) {
    ctx.body = {
      result,
      meta: {
        msg: "请求数据成功",
        code: 200
      }
    }
  } else {
    ctx.body = {
      result,
      meta: {
        msg: "当前没有该宿舍卫生文化信息",
        code: 201
      }
    }
  }
})
export default router
